package com.ffcs.cn.action.labelBase.runprocess;

import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.utilties.DoubleUtilities;
import com.supermap.desktop.core.utilties.FileUtilities;
import com.supermap.desktop.core.utilties.StringUtilities;
import com.supermap.desktop.core.utilties.WorkflowUtilities;
import com.supermap.iobjects.common.convertor.Convertors;
import com.supermap.iobjects.common.convertor.IConvertor;
import com.supermap.sps.core.parameters.AbstractValueProvider;
import com.supermap.sps.core.parameters.IInput;
import com.supermap.sps.core.parameters.IPreservableValueProvider;
import com.supermap.sps.core.parameters.ISingleDataDefinition;

import java.io.File;


public class SampleCodeSingleValueProvider extends AbstractValueProvider implements IPreservableValueProvider {
    private final ISingleDataDefinition singleDataDefinition;
    private IInput input;
    private Object value;

    public SampleCodeSingleValueProvider(IInput input, Object value) {
        this.input = input;
        this.value = value;
        this.singleDataDefinition = (ISingleDataDefinition) input.getDataDefinition();
    }

    @Override
    public ISingleDataDefinition getDataDefinition() {
        return this.singleDataDefinition;
    }

    @Override
    public boolean isReady() {
        if (this.input.isRequired()) {
            return !StringUtilities.isNullOrEmptyString(value);
        } else {
            return true;
        }
    }

    @Override
    public String getTextValue() {
        if (WorkflowUtilities.isInlineParameterText(value)) {
            return value.toString();
        }
        return null;
    }

    @Override
    protected Object innerGetValue() {
        Class<?> classType = this.singleDataDefinition.getType();
        try {
            if (value != null) {
                if (classType.isAssignableFrom(value.getClass())) {
                    return value;
                } else if (!value.getClass().equals(classType)) {
                    if (value.getClass() == String.class) {
                        if (StringUtilities.isNullOrEmpty(value.toString())) {
                            return null;
                        } else if (classType == Byte.class || classType == byte.class) {
                            value = Byte.parseByte(value.toString());
                        } else if (classType == Short.class || classType == short.class) {
                            value = Short.parseShort(value.toString());
                        } else if (classType == Integer.class || classType == int.class) {
                            value = Integer.parseInt(value.toString());
                        } else if (classType == Long.class || classType == long.class) {
                            value = DoubleUtilities.stringToValue(value.toString()).longValue();
                        } else if (classType == Float.class || classType == float.class) {
                            value = DoubleUtilities.stringToValue(value.toString()).floatValue();
                        } else if (classType == Double.class || classType == double.class) {
                            value = DoubleUtilities.stringToValue(value.toString());
                        } else if (classType == File.class && FileUtilities.isShareFolderRootFile(String.valueOf(value))) {
                            // 单独处理下共享路径，否则走ReverseConvertor会拼接出来一个错误值
                            value = new File(value.toString());
                        } else {
                            IConvertor convertor = Convertors.instance().find(String.class, classType);
                            Object converted = convertor.valueTo(value);
                            return converted;
                        }
                    } else if (value.getClass() == Integer.class) {
                        if (classType == Boolean.class || classType == boolean.class) {
                            int i = Integer.parseInt(value.toString());
                            value = i != 0;
                        }
                    } else {
                        IConvertor convertor = Convertors.instance().find(value.getClass(), classType);
                        Object converted = convertor.valueTo(value);
                        return converted;
                    }
                }
            }
        } catch (Exception e) {
            // 值转换失败，返回空
            Application.getActiveApplication().getOutput().output(e.getMessage());
            return null;
        }
        return value;
    }
}
